﻿/****************************** Module Header ******************************\
 * Module Name:  SoLoading.cs
 * Module Version:  1.0.0 (2012-11-13)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension winformcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCF.Core.Forms.CustomControls.SoLoading
{
    public partial class frmSoLoading : Form
    {
        //首先定义私有变量
        private Image m_img = null;
        private EventHandler evtHandler = null;
        public frmSoLoading()
        {
            InitializeComponent();
        }
        public frmSoLoading(Image gif, bool isTransparen=false)
            : this()
        {
            m_img = gif;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Width = m_img.Width + 16;
            this.Height = m_img.Height + 16;
            if (isTransparen)
            {
                this.TransparencyKey = Color.Black;
                this.BackColor = Color.Black;
            }
        }

        public void Resize(int left, int top)
        {
            this.StartPosition = FormStartPosition.Manual;
            this.Top = top;
            this.Left = left;
        }

        public void Resize(int left, int top,Color bgColor)
        {
            this.Resize(left, top);
            this.BackColor = bgColor;
        }

        //重载当前winform的OnPaint方法，当界面被重绘时去显示当前gif显示的某一帧
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (m_img != null)
            {
                //获得当前gif动画下一步要渲染的帧。
                UpdateImage();
                //将获得的当前gif动画需要渲染的帧显示在界面上的某个位置。
                e.Graphics.DrawImage(m_img, new Rectangle(8, 8, m_img.Width, m_img.Height));
            }
        }

        //实现Load方法
        private void frmSoLoading_Load(object sender, EventArgs e)
        {
            this.Height = m_img.Height + 16;
            //为委托关联一个处理方法
            evtHandler = new EventHandler(OnImageAnimate);
            //调用开始动画方法
            BeginAnimate();
        }
        private void BeginAnimate()//开始动画方法
        {
            if (m_img != null)
            {
                //当gif动画每隔一定时间后，都会变换一帧那么就会触发一事件，该方法就是将当前image每变换一帧时，都会调用当前这个委托所关联的方法。
                ImageAnimator.Animate(m_img, evtHandler);
            }
        }

        //委托所关联的方法
        private void OnImageAnimate(Object sender, EventArgs e)
        {
            //该方法中，只是使得当前这个winform重绘，然后去调用该winform的OnPaint（）方法进行重绘)
            this.Invalidate();
        }
        private void UpdateImage()
        {
            //获得当前gif动画的下一步需要渲染的帧，当下一步任何对当前gif动画的操作都是对该帧进行操作
            ImageAnimator.UpdateFrames(m_img);
        }
        private void StopAnimate()
        {
            //关闭显示动画，该方法可以在winform关闭时，或者某个按钮的触发事件中进行调用，以停止渲染当前gif动画。
            m_img = null;
            ImageAnimator.StopAnimate(m_img, evtHandler);
        }

        //关闭动画
        private void frmSoLoading_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopAnimate();
        }
    }
}
